﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.JShop.UI;
using System.Data;
using CNVP.JShop.Web.Controls;
using CNVP.Framework;
using CNVP.Framework.Utils;
using CNVP.Framework.Helper;

namespace CNVP.JShop.Web.Admin
{
    public partial class ProductListUp : AdminPage
    {
        BLL.CNVP_Products bll = new BLL.CNVP_Products();
        public int rplistCount = 0;
        public int PageIndex = 1;
        public string SearchType = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!BLL.BLLCheckAdmin.LoginOk(Page, 115)) return;//判断页面权限
            if (!string.IsNullOrEmpty(Request.QueryString["Page"]) && Public.IsNumber(Request.QueryString["Page"]))
            {
                PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            }
            if (!string.IsNullOrEmpty(Request.QueryString["SearchType"]))
            {
                SearchType = Request.QueryString["SearchType"];
            }
            if (!IsPostBack)
            {
                ViewState["SearchType"] = "0";
                ProductClassIDBind();
                ddlProductTypeIDBind();
                StartLoad(PageIndex);
                #region 验证
                ddlProductTypeID.Attributes.Add("onchange", "ProTypeRefBranks();");
                btDelete.Attributes.Add("onclick", "if(ifCheck()){return confirm('确定删除该记录吗?');}else{alert('请选择记录！');return false;};");
                btDown.Attributes.Add("onclick", "if(ifCheck()){return confirm('确定下架该记录吗?');}else{alert('请选择记录！');return false;};");
                #endregion
            }
        }

        //商品分类
        private void ProductClassIDBind()
        {
            new BLL.BLLColumn().BuildAllDDList(this.ddlProductClassID,3);
        }
        //商品类型
        private void ddlProductTypeIDBind()
        {
            ddlProductTypeID.DataSource = new BLL.CNVP_ProductType().LoadAllUseDropDownList();
            ddlProductTypeID.DataTextField = "ProTypeName";
            ddlProductTypeID.DataValueField = "ID";
            ddlProductTypeID.DataBind();
        }

        /// <summary>
        /// PageNavigator1_PageChange 的摘要说明
        /// 分页加载列表函数
        /// </summary>
        protected void StartLoad(int PageIndex)
        {
            SQLConditionInfo[] st = new SQLConditionInfo[12];
            st[0] = new SQLConditionInfo("@IsDelete", "0");
            st[1] = new SQLConditionInfo("@key", Public.FilterSql(txtkey.Text.Trim()));
            st[2] = new SQLConditionInfo("@ProductClassID", ddlProductClassID.SelectedValue);
            st[3] = new SQLConditionInfo("@ProductTypeID", ddlProductTypeID.SelectedValue);
            st[4] = new SQLConditionInfo("@key1", Public.FilterSql(txtkey1.Text.Trim()));
            st[6] = new SQLConditionInfo("@BrandID", Request.Form["listBranks"]);
            st[10] = new SQLConditionInfo("@ProductState", "1");
            if (SearchType == "1")
            {
                string ProductClassID_Str = "";
                if (!string.IsNullOrEmpty(Request.QueryString["ProductClassID"]))
                {
                    ViewState["SearchType"] = "2";
                    ProductClassID_Str = Request.QueryString["ProductClassID"];
                    st[7] = new SQLConditionInfo("@ProductClassID", ProductClassID_Str);
                }
                string ProductTypeID_Str = "";
                if (!string.IsNullOrEmpty(Request.QueryString["ProductTypeID"]))
                {
                    ViewState["SearchType"] = "3";
                    ProductTypeID_Str = Request.QueryString["ProductTypeID"];
                    st[8] = new SQLConditionInfo("@ProductTypeID", ProductTypeID_Str);
                }
                string BrandID_Str = "";
                if (!string.IsNullOrEmpty(Request.QueryString["BrandID"]))
                {
                    ViewState["SearchType"] = "4";
                    BrandID_Str = Request.QueryString["BrandID"];
                    st[9] = new SQLConditionInfo("@BrandID", BrandID_Str);
                }
            }
            if (ViewState["SearchType"].ToString() == "5")
            {
                st[11] = new SQLConditionInfo("@ProductState", ViewState["ProductState"]);
            }
            st[5] = new SQLConditionInfo("@SearchType", ViewState["SearchType"]);
            int i, j;
            DataTable dt = null;
            int pageNum = 20;
            dt = bll.GetPage(this.GetType().Name, PageIndex, pageNum, out i, out j, st);
            if (dt != null)
            {
                rplistCount = dt.Rows.Count;//行数
                //----------------------------------------添加列------------------------------------------------
                dt.Columns.Add("oProductName", typeof(string));
                dt.Columns.Add("oProductClassID", typeof(string));
                dt.Columns.Add("oProductTypeID", typeof(string));
                dt.Columns.Add("oBrandID", typeof(string));
                dt.Columns.Add("oStockTotalNum", typeof(string));
                dt.Columns.Add("oProductState", typeof(string));
                //----------------------------------------添加列结束--------------------------------------------
                for (int k = 0; dt.Rows.Count > k; k++)
                {
                    dt.Rows[k]["oProductName"] = BasePage.CutString(dt.Rows[k]["ProductName"].ToString(), 30);
                    string _ColumnName = "";
                    var model_Column=new BLL.CNVP_Column().GetSingle_CNVP_ColumnById(Convert.ToInt32(dt.Rows[k]["ProductClassID"]));
                    if (model_Column != null)
                    {
                        _ColumnName = model_Column.ColumnName;
                    }
                    dt.Rows[k]["oProductClassID"] = _ColumnName;
                    string _ProTypeName = "";
                    var model_ProductType=new BLL.CNVP_ProductType().GetSingle_CNVP_ProductTypeById(Convert.ToInt32(dt.Rows[k]["ProductTypeID"]));
                    if (model_ProductType != null)
                    {
                        _ProTypeName = model_ProductType.ProTypeName;
                    }
                    dt.Rows[k]["oProductTypeID"] = _ProTypeName;
                    string oBrandID = "";
                    try
                    {
                        oBrandID = new BLL.CNVP_Branks().GetSingle_CNVP_BranksById(Convert.ToInt32(dt.Rows[k]["BrandID"])).BrankName;
                    }
                    catch { oBrandID = ""; }
                    dt.Rows[k]["oBrandID"] = oBrandID;
                    int StockNum = 0;
                    var ProductsDetail_List = new BLL.CNVP_ProductsDetail().GetAll_CNVP_ProductsDetail(Convert.ToInt32(dt.Rows[k]["ID"]));
                    for (int a = 0; a < ProductsDetail_List.Count; a++)
                    {
                        StockNum += ProductsDetail_List[a].StockNum;
                    }
                    dt.Rows[k]["oStockTotalNum"] = StockNum;
                    if (dt.Rows[k]["ProductState"].ToString() == "1")
                    {
                        dt.Rows[k]["oProductState"] = "上架";
                    }
                    else
                    {
                        dt.Rows[k]["oProductState"] = "<font color=\"red\">下架</font>";
                    }
                }
            }
            rpList.DataSource = dt;
            rpList.DataBind();
            paging.InnerHtml = Paginator.Pagination(PageIndex, i, j);
        }
        protected void btDelete_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.rpList.Items.Count; i++)
            {
                CheckBox cbCheck = (CheckBox)this.rpList.Items[i].FindControl("cbCheck");
                if (cbCheck.Checked == true)
                {
                    Label lbID = (Label)this.rpList.Items[i].FindControl("lbID");
                    int code = int.Parse(lbID.Text);
                    Model.CNVP_Products model = new Model.CNVP_Products();//放入回收站
                    model.ID = code;
                    model.IsDelete = 1;
                    bll.Update_CNVP_ProductsByIsDelete(model);
                }
            }
            StartLoad(PageIndex);
        }
        protected void btClearBox_Click(object sender, EventArgs e)
        {
            Response.Redirect("ProductListClear.aspx");
        }
        protected void btSearch_Click(object sender, ImageClickEventArgs e)
        {
            SearchType = "2";
            ViewState["SearchType"] = "0";
            StartLoad(PageIndex);
        }
        protected void btHighSearch_Click(object sender, EventArgs e)
        {
            SearchType = "2";
            ViewState["SearchType"] = "1";
            StartLoad(PageIndex);
        }

        protected void btDown_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.rpList.Items.Count; i++)
            {
                CheckBox cbCheck = (CheckBox)this.rpList.Items[i].FindControl("cbCheck");
                if (cbCheck.Checked == true)
                {
                    int code = int.Parse(cbCheck.ToolTip);
                    Model.CNVP_Products model = new Model.CNVP_Products();//下架
                    model.ID = code;
                    model.ProductState = 0;
                    bll.UpdateByProductState(model);
                }
            }
            StartLoad(PageIndex);
        }
    }
}
